package com.common.base.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImgUrlUtils {
    public static final int TYPE_QN = 0;
    public static final int TYPE_AL = 1;
    public static final int TYPE_HW = 2;

    /**
     *
     */
    public static int checkType(String mUrl) {
        int osType;

        if (mUrl.contains("?x-oss-process=image/")) {
            osType = TYPE_AL;
        } else if (mUrl.contains("?x-image-process=image/")) {
            osType = TYPE_HW;
        } else {
            osType = TYPE_QN;
        }
        return osType;
    }

    /**
     *
     */
    public static String getOne2One(String mUrl) {
        String urlTemp = "";
        if (TextUtils.isEmpty(mUrl)) {
            return urlTemp;
        }
        if (mUrl.contains("?imageView2/0/") || mUrl.contains("?x-oss-process=image/") || mUrl.contains("?x-image-process=image/")) {
            return urlTemp;
        }

        return ImgUrlUtils.getOne2One(ImgUrlUtils.checkType(mUrl), mUrl);
    }

    /**
     *
     */
    public static String getOne2One(int osType, String mUrl) {
        String urlTemp = "";
        if (TextUtils.isEmpty(mUrl)) {
            return urlTemp;
        }

        return ImgUrlUtils.getOne2One(osType, mUrl, 300,"webp");
    }


    /**
     *
     */
    public static String getOne2One(int osType, String mUrl, int mWH) {
        String urlTemp = "";
        if (TextUtils.isEmpty(mUrl)) {
            return urlTemp;
        }

        return ImgUrlUtils.getOne2One(osType, mUrl, mWH,"webp");
    }


    /**
     *
     */
    public static String getOne2One(int osType, String mUrl, int mWH, int mH) {
        return ImgUrlUtils.getOne2One(osType, mUrl, mWH, mH, "webp");
    }

    /**
     *
     */
    public static String getOne2One(int osType, String mUrl, int mWH, String format) {
        return ImgUrlUtils.getOne2One(osType, mUrl, mWH, mWH, format);
    }

    /**
     *
     */
    public static String getOne2One(int osType, String mUrl, int mW, int mH, String format) {
        String urlTemp = "";
        if (TextUtils.isEmpty(mUrl)) {
            return urlTemp;
        }

        switch (osType) {
            case TYPE_QN:
                if (mH <= -1) {
                    urlTemp = mUrl + "?imageView2/0/w/" + mW;
                } else {
                    urlTemp = mUrl + "?imageView2/0/w/" + mW + "/h/" + mH;
                }
                break;
            case TYPE_AL:
                if (mH <= -1) {
                    urlTemp = mUrl + "?x-oss-process=image/resize,w_" + mW + "/format," + format;
                } else {
                    urlTemp = mUrl + "?x-oss-process=image/crop,w_" + mW + ",h_" + mH + ",g_center" + "/format," + format;
                }
                break;
            case TYPE_HW:
                if (mH <= -1) {
//                    urlTemp = mUrl + "?x-image-process=image/resize,m_lfit,w_" + mW + ",h_" + mW + "/format," + format;
                    urlTemp = mUrl + "?x-image-process=image/resize,m_pad,w_" + mW + ",h_" + mW +",color_FF0000"+ "/format," + format+ "/imageslim";
                } else {
                    urlTemp = mUrl + "?x-image-process=image/crop,w_" + mW + ",h_" + mH + ",g_center" + "/format," + format+ "/imageslim";
                }
                break;
            default:
                urlTemp = mUrl;
                break;
        }

        return urlTemp;
    }


    /**
     *按宽等比缩放
     */
    public static String getWidthAutoScale(int osType, String mUrl, int mW, String format) {
        String urlTemp = "";
        if (TextUtils.isEmpty(mUrl)) {
            return urlTemp;
        }

        switch (osType) {
            case TYPE_QN:
                urlTemp = mUrl + "?imageView2/0/w/" + mW;
                break;
            case TYPE_AL:
                urlTemp = mUrl + "?x-oss-process=image/resize,w_" + mW + ",m_lfit,g_center/format," + format;
                break;
            case TYPE_HW:
                urlTemp = mUrl + "?x-image-process=image/resize,m_pad,w_" + mW +",color_FF0000"+ "/format," + format+ "/imageslim";
                break;
            default:
                urlTemp = mUrl;
                break;
        }

        return urlTemp;
    }

    /**
     * 获取图片字符串中所有链接
     */
    public static Set<String> getImgStr(String htmlStr) {
        Set<String> pics = new HashSet<>();

        if (TextUtils.isEmpty(htmlStr)) {
            return pics;
        }
        String img = "";
        Pattern p_image;
        Matcher m_image;
        //     String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile
                (regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
            }
        }
        return pics;
    }

    public static List<String> getImgSrc(String content) {
        List<String> list = new ArrayList<String>();

        if (TextUtils.isEmpty(content)) {
            return list;
        }

        //目前img标签标示有3种表达式
        //<img alt="" src="1.jpg"/>   <img alt="" src="1.jpg"></img>     <img alt="" src="1.jpg">
        //开始匹配content中的<img />标签
        Pattern p_img = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
        Matcher m_img = p_img.matcher(content);
        boolean result_img = m_img.find();
        if (result_img) {
            while (result_img) {
                //获取到匹配的<img />标签中的内容
                String str_img = m_img.group(2);

                //开始匹配<img />标签中的src
                Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
                Matcher m_src = p_src.matcher(str_img);
                if (m_src.find()) {
                    String str_src = m_src.group(3);
                    list.add(str_src);
                }
                //结束匹配<img />标签中的src

                //匹配content中是否存在下一个<img />标签，有则继续以上步骤匹配<img />标签中的src
                result_img = m_img.find();
            }
        }
        return list;

    }

    /**
     *
     */
    public static String checkHasDomain(String mUrl) {
        if (TextUtils.isEmpty(mUrl)) {
            return "";
        }
        if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
            return mUrl;
        }
        String urlTemp = "";
        if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
            return mUrl;
        }
//        urlTemp = "https://" + Constants.bucket + "." + Constants.endpoint + "/" + mUrl;
        urlTemp = "https://new-static-test.faxian.ink/" + mUrl;
        return urlTemp;
    }
}
